//
//  GWViewController.m
//  HLEM3u8DownloadManager
//
//  Created by 134344 on 09/01/2020.
//  Copyright (c) 2020 134344. All rights reserved.
//

#import "GWViewController.h"
#import <HLEM3u8DownloadManager/HLEM3u8DownloadManager.h>

@interface GWViewController ()

@property(nonatomic,strong)HLEM3U8DownloadTask *task;

@end

@implementation GWViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (IBAction)createTask:(UIButton *)sender {
    NSString *homeDir = NSHomeDirectory();
    NSLog(@"=====%@",homeDir);
    //http://cctvalih5ca.v.myalicdn.com/live/cctv1_2/index.m3u8
    NSLog(@"Thread=====%@",[NSThread currentThread]);
    [HLEM3u8DownloadManager.standardUserDefaults creatTaskById:100 downloadUrl:@"http://cctvalih5ca.v.myalicdn.com/live/cctv1_2/index.m3u8" fileSavePath:@"Documents/100" success:^(HLEM3U8DownloadTask *task){
        self.task = task;
        NSLog(@"创建任务成功===>%@",self.task.name);
        NSLog(@"Thread=====%@",[NSThread currentThread]);
        [self downloadFiles];
    } failure:^(HLEM3U8DownloadTask *task,NSError * _Nonnull error) {
        NSLog(@"===>%@",error);
        NSLog(@"Thread=====%@",[NSThread currentThread]);
    }];
    
}

- (void)downloadFiles{
    [HLEM3u8DownloadManager.standardUserDefaults resumeTask:self.task progress:^(HLEM3U8DownloadTask *task,NSProgress * _Nonnull progress, NSInteger allSize) {
        NSLog(@"下载的进度:%f",progress.completedUnitCount / (progress.totalUnitCount * 1.0));
        NSLog(@"文件总大小:%zd",allSize);
        NSLog(@"Thread=====%@",[NSThread currentThread]);
    } speed:^(HLEM3U8DownloadTask *task,NSInteger speed) {
        NSLog(@"下载速度:%zd b/s",speed);
        NSLog(@"Thread=====%@",[NSThread currentThread]);
    } success:^(HLEM3U8DownloadTask *task){
        NSLog(@"下载成功");
        NSLog(@"Thread=====%@",[NSThread currentThread]);
        [self combineMP4];
    } failure:^(HLEM3U8DownloadTask *task,NSError * _Nonnull error) {
        NSLog(@"下载失败:%@",error.localizedDescription);
        NSLog(@"Thread=====%@",[NSThread currentThread]);
    }];
    
}

- (void)combineMP4{
    
    [HLEM3u8DownloadManager.standardUserDefaults combineMP4ByTaskID:self.task.taskID tsFilePath:@"Documents/100" mp4FilePath:@"Library/Caches/HLEM3u8DownloadFile/ff13cf749a2c489c8c2267f3972c9c59/2b9857cb46fc429db598ac014213a2b4/17820.mp4" success:^(HLEM3U8DownloadTask * _Nonnull task) {
        NSLog(@"合并转码成功");
        NSLog(@"Thread=====%@",[NSThread currentThread]);
    } failure:^(HLEM3U8DownloadTask * _Nonnull task, NSError * _Nonnull error) {
        NSLog(@"合并转码失败:%@",error.localizedDescription);
        NSLog(@"Thread=====%@",[NSThread currentThread]);
    }];
}
- (IBAction)pause:(UIButton*)sender {
    NSLog(@"Thread11=====%@",[NSThread currentThread]);
    if (sender.selected == YES) {
        NSLog(@"开启");
        [HLEM3u8DownloadManager.standardUserDefaults resumeTaskBytaskID:self.task.taskID progress:^(HLEM3U8DownloadTask * _Nonnull task, NSProgress * _Nonnull progress, NSInteger allSize) {
            NSLog(@"下载的进度:%f",progress.completedUnitCount / (progress.totalUnitCount * 1.0));
            NSLog(@"文件总大小:%zd",allSize);
            NSLog(@"Thread=====%@",[NSThread currentThread]);
        } speed:^(HLEM3U8DownloadTask * _Nonnull task, NSInteger speed) {
            NSLog(@"下载速度:%zd b/s",speed);
            NSLog(@"Thread=====%@",[NSThread currentThread]);
        } success:^(HLEM3U8DownloadTask * _Nonnull task) {
            NSLog(@"下载成功");
            NSLog(@"Thread=====%@",[NSThread currentThread]);
            [self combineMP4];
        } failure:^(HLEM3U8DownloadTask * _Nonnull task, NSError * _Nonnull error) {
            NSLog(@"下载失败:%@",error.localizedDescription);
            NSLog(@"Thread=====%@",[NSThread currentThread]);
        }];
    }else{
        NSLog(@"暂停");
        [HLEM3u8DownloadManager.standardUserDefaults pauseTask:self.task];
    }
    sender.selected = !sender.selected;
    
}

- (IBAction)cancel:(UIButton *)sender {
    NSLog(@"====>任务取消");
    NSLog(@"====%d",[HLEM3u8DownloadManager.standardUserDefaults cancelTask:self.task]);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
